package com.hn.kiss.modules.sys.service.impl;

import com.hn.kiss.modules.sys.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class EmailServiceimpl implements IEmailService {
    @Value("${spring.mail.username}")
    private String fromUser;
    @Value("${project.email.subject}")
    private String subject;
    @Value("${project.email.login-text}")
    private String loginText;

    @Autowired
    private JavaMailSender mailSender;


    @Override
    public String loginVerificationCode(String email) throws MailException {
        Random r = new Random();
        String verficationCode =String.valueOf(1000 + r.nextInt(8999));

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromUser);
        message.setTo(email);
        message.setSubject(subject);
        message.setText(loginText.replace("xxxx", verficationCode));
        mailSender.send(message);
        return verficationCode;
    }

    public static void main(String[] args) {
        String str = "你好,xxxx,我的时间";
        System.out.println(str.replace("xxxx", "1234"));
    }
}
